<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <template v-for="(item, index) in levelList" :key="item.path">
        <el-breadcrumb-item v-if="item.meta.title">
          <span
            v-if="item.redirect === 'noredirect' || index == levelList.length - 1"
            class="no-redirect"
            >{{ item.meta.title }}</span
          >
          <!-- <router-link v-else :to="item.redirect||item.path">{{item.meta.title}}</router-link> -->
          <span v-else>{{ item.meta.title }}</span>
        </el-breadcrumb-item>
      </template>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import { getTenantInfo } from '@/api/tenant'
import { createNamespacedHelpers } from 'vuex'
const { mapState } = createNamespacedHelpers('user')

export default {
  data() {
    return {
      levelList: null
    }
  },
  watch: {
    $route() {
      this.getBreadcrumb()
    }
  },
  created() {
    this.getBreadcrumb()
  },
  // eslint-disable-next-line vue/order-in-components
  computed: {
    ...mapState(['roles'])
  },
  methods: {
    getBreadcrumb() {
      let matched = this.$route.matched.filter(item => item.name)
      const first = matched[0]
      if (this.roles[0] !== 'SYS_ADMIN' && !this.$store.state.app.sysSign) {
        getTenantInfo(this.$store.state.user.tenantId).then(rel => {
          const companyName = rel.data.name
          if (first && first.name !== 'dashboard') {
            matched = [{ path: '/dashboard', meta: { title: companyName } }].concat(matched)
          }
          this.levelList = matched
        })
      } else {
        if (first && first.name !== 'dashboard') {
          matched = [{ path: '/dashboard', meta: { title: '' } }].concat(matched)
        }
        this.levelList = matched
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 60px;
  margin-left: 10px;
}
</style>
